package com.thread.test;

/**
 * yield此方法就是让出自己没有用完的时间片  让别的线程运行
 */
public class Yield implements  Runnable{


    @Override
    public void run() {
        while (!Thread.currentThread().interrupted()) {
            for(int i=0;i<5;i++){
                if(i%5==0){
                    System.out.println(Thread.currentThread()+"yield cpu..");
                    Thread.yield();
                }
            }
        }

        System.out.println(Thread.currentThread()+"is over---"+Thread.currentThread().isInterrupted());
    }

    public Yield() {
        Thread thread = new Thread(this);
        thread.start();

//        thread.isInterrupted();
//        thread.interrupted();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
//        boolean interrupted3 = Thread.interrupted();
//        System.out.println("3-------------"+interrupted3);
//        boolean interrupted2 = thread.isInterrupted();
//        System.out.println("2-------------"+interrupted2);
//        boolean interrupted = thread.interrupted();
//        System.out.println("----------"+interrupted);
        try {
            thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        boolean interrupted1 = thread.isInterrupted();
        System.out.println("1-------------"+interrupted1);


    }
/*

    public static void main(String[] args) {
         new Yield();
//        new Yield();
//        new Yield();
    }
*/

//    运行结果
// Thread-1线程名 三个线程0-1-2   5代表默认的优先级public final static int NORM_PRIORITY = 5;
   /* Thread[Thread-1,5,main]yield cpu..
    Thread[Thread-1,5,main]is over
    Thread[Thread-2,5,main]yield cpu..
    Thread[Thread-2,5,main]is over
    Thread[Thread-0,5,main]yield cpu..
    Thread[Thread-0,5,main]is over*/
}
